* Begin log file
capture log close sublog
log using "$projdir/log/6_mcd_stack.txt", name(sublog) text replace

*-------------------------------------------------------------------------------
* Description: Stack and average the pandemic-corrected synthetic control results
* for (1) McD subsample of QCEW and (2) McD price data for subsample of counties
* Author: Denis Sosinskiy
*
* Updated: August 8, 2023
*-------------------------------------------------------------------------------
clear

*-------------------------------------------------------------------------------
* All treated counties, McD data
*-------------------------------------------------------------------------------
foreach y in hwage price { 
		
	* Ensure necessary folders exists
	capture mkdir "$projdir/dta/analysis/mcd/`y'/agg_att_subsample"
	capture mkdir "$projdir/dta/analysis/mcd/`y'/agg_att_subsample/bias_corrected"
	capture mkdir "$projdir/dta/analysis/mcd/`y'/agg_att_subsample/covid_corrected"
			
			
	* Add trunit and trperiod to the uncorrected individual estimates if needed
	foreach z in California NY {
			
		* Set trperiod macro
		local _trp = 2014
			
		* Get filelist
		local filelist: dir "$projdir/dta/analysis/mcd/`y'/`z'_subsample" files "*.dta"
				
		* Add the variables if needed
		foreach f of local filelist {
			if strpos("`f'", "_ate") == 0 {
				qui use "$projdir/dta/analysis/mcd/`y'/`z'_subsample/`f'", clear
						
				* Extract and create trunit
				capture assert trunit
				if _rc != 0 {
					local _trunit = subinstr("`f'", "tr_", "", .)
					local _trunit = subinstr("`_trunit'", ".dta", "", .)
					qui gen trunit = `_trunit'
				}
				* Exclude mid counties in NY
				if !inlist(`_trunit', 36007, 36027, 36063, 36065, 36069, 36083, 36085, 36087, 36091, 36093, 36111) {
					local inclunits "`inclunits' `_trunit'"
				}
						
				* Generate trperiod
				capture assert trperiod
				if _rc != 0 {
					qui gen trperiod = `_trp'
				}
						
				* Save the file
				qui save "$projdir/dta/analysis/mcd/`y'/`z'_subsample/`f'", replace
			}
		}
	}

	* Uncorrected estimates
	#delimit ;
		stackscpvals, 
			gap(p_gap) 
			time(_time) 
			unit(cty_fips) 
			pvalues(rmspe variance) 
			avgwts(pop10)
			emin(-5) emax(20) 
			filepath("$projdir/dta/analysis/mcd/`y'/California_subsample") 
			filepath_2("$projdir/dta/analysis/mcd/`y'/NY_subsample")
			savepath("$projdir/dta/analysis/mcd/`y'/agg_att_subsample")
			savename(agg_att);
		#delimit cr
				
}

*-------------------------------------------------------------------------------
* Excluding treated counties with local minimum wages, McD data
*-------------------------------------------------------------------------------
foreach y in hwage price  {	
	local n = 722513
		
	* Ensure necessary folders exists
	capture mkdir "$projdir/dta/analysis/mcd/`y'/nolocal_att_subsample"
	capture mkdir "$projdir/dta/analysis/mcd/`y'/nolocal_att_subsample/bias_corrected"
	capture mkdir "$projdir/dta/analysis/mcd/`y'/nolocal_att_subsample/covid_corrected"
		
	* Generate trunit and trperiod variables if needed, and build inclunits list
	local inclunits
	foreach z in California NY {
		
		* Set trperiod macro
		local _trp = 218
		if "`z'" == "NY" {
			local _trp = 216
		}
		
		* Get filelist
		local filelist: dir "$projdir/dta/analysis/mcd/`y'/`z'_subsample" files "*.dta"
			
		* Add the variables if needed, and add the appropriate counties to the macro
		foreach f of local filelist {
			if strpos("`f'", "_ate") == 0 {
				qui use "$projdir/dta/analysis/mcd/`y'/`z'_subsample/`f'", clear
					
				* Extract and create trunit (if necessary) and add to inclunits list
				local _trunit = subinstr("`f'", "tr_", "", .)
				local _trunit = subinstr("`_trunit'", ".dta", "", .)
				capture assert trunit
				if _rc != 0 {
					qui gen trunit = `_trunit'
				}
				* Exclude counties with local minimum wages
				if !inlist(`_trunit', 6001, 6013, 6037, 6041, 6073, 6075, 6081, 6085, 6097, 36005, 36047, 36061, 36081, 36085) {
					local inclunits "`inclunits' `_trunit'"
				}
					
				* Generate trperiod
				capture assert trperiod
				if _rc != 0 {
					qui gen trperiod = `_trp'
				}
					
				* Save the file
				qui save "$projdir/dta/analysis/mcd/`y'/`z'_subsample/`f'", replace
			}
		}
	}
		
		* Uncorrected estimates
		#delimit ;
			stackscpvals, 
				gap(p_gap) 
				time(_time) 
				unit(cty_fips) 
				pvalues(rmspe variance) 
				avgwts(pop10)
				emin(-5) emax(20) 
				filepath("$projdir/dta/analysis/mcd/`y'/California_subsample") 
				filepath_2("$projdir/dta/analysis/mcd/`y'/NY_subsample")
				savepath("$projdir/dta/analysis/mcd/`y'/nolocal_att_subsample")
				keeptrunits(`inclunits');
			#delimit cr
}


*-------------------------------------------------------------------------------
* All treated counties, QCEW data
*-------------------------------------------------------------------------------
clear

local y = "avg_wkly_wage" 	
local n = 722513
		
* Ensure necessary folders exists
capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att_subsample"
capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att_subsample/bias_corrected"
capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att_subsample/covid_corrected"
		
* Add trunit and trperiod to the uncorrected individual estimates if needed
foreach z in California NY {
		
	* Set trperiod macro
	local _trp = 218
	if "`z'" == "NY" {
		local _trp = 216
	}
		
	* Get filelist
	local filelist: dir "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'_subsample" files "*.dta"
			
	* Add the variables if needed
	foreach f of local filelist {
		if strpos("`f'", "_ate") == 0 {
			qui use "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'_subsample/`f'", clear
			sort cty_fips _Co_Number
					
			* Extract and create trunit
			capture assert trunit
			if _rc != 0 {
				local _trunit = subinstr("`f'", "tr_", "", .)
				local _trunit = subinstr("`_trunit'", ".dta", "", .)
				qui gen trunit = `_trunit'
			}
					
			* Generate trperiod
			capture assert trperiod
			if _rc != 0 {
				qui gen trperiod = `_trp'
			}
					
			* Save the file
			qui save "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'_subsample/`f'", replace
		}
	}
}

* Uncorrected estimates
#delimit ;
	stackscpvals, 
		gap(gap) 
		time(_time) 
		unit(cty_fips) 
		pvalues(rmspe variance) 
		avgwts(pop10)
		emin(-17) emax(33) 
		filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California_subsample") 
		filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY_subsample")
		savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att_subsample");
	#delimit cr
		
* Bias-corrected estimates
#delimit ;
	stackscpvals, 
		gap(gap_bc) 
		time(_time) 
		unit(cty_fips) 
		pvalues(rmspe variance) 
		avgwts(pop10)
		emin(-17) emax(33) 
		filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California_subsample") 
		filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY_subsample")
		savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att_subsample/bias_corrected")
		savename("bias_corr_att");
	#delimit cr
		
* Covid-corrected estimates
#delimit ;
	stackscpvals, 
		gap(p_gap) 
		time(_time) 
		unit(cty_fips) 
		pvalues(rmspe variance) 
		avgwts(pop10)
		emin(-17) emax(33) 
		filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California_subsample/corrected") 
		filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY_subsample/corrected")
		savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/agg_att_subsample/covid_corrected")
		savename("covid_corr_att");
	#delimit cr


*-------------------------------------------------------------------------------	
* Excluding treated counties with local minimum wages
*-------------------------------------------------------------------------------
local y = "avg_wkly_wage" 	
local n = 722513
		
* Ensure necessary folders exists
capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att_subsample"
capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att_subsample/bias_corrected"
capture mkdir "$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att_subsample/covid_corrected"

*Add trunit and trperiod to the uncorrected individual estimates if needed
foreach z in California NY {
		
	* Set trperiod macro
	local _trp = 218
	if "`z'" == "NY" {
		local _trp = 216
	}
		
	* Get filelist
	local filelist: dir "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'_subsample" files "*.dta"
			
	* Add the variables if needed
	foreach f of local filelist {
		if strpos("`f'", "_ate") == 0 {
			qui use "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'_subsample/`f'", clear
			sort cty_fips _Co_Number
					
			* Extract and create trunit
			capture assert trunit
			if _rc != 0 {
				local _trunit = subinstr("`f'", "tr_", "", .)
				local _trunit = subinstr("`_trunit'", ".dta", "", .)
				qui gen trunit = `_trunit'
			}
					
			* Generate trperiod
			capture assert trperiod
			if _rc != 0 {
				qui gen trperiod = `_trp'
			}
			
			* Exclude counties with local minimum wages
			if !inlist(`_trunit', 6001, 6013, 6037, 6041, 6073, 6075, 6081, 6085, 6097, 36005, 36047, 36061, 36081, 36085) {
				local inclunits "`inclunits' `_trunit'"
			}
					
			* Save the file
			qui save "$projdir/dta/analysis/qcew/`y'/naics`n'/`z'_subsample/`f'", replace
		}
	}
}

* Uncorrected estimates
#delimit ;
	stackscpvals, 
		gap(gap) 
		time(_time) 
		unit(cty_fips) 
		pvalues(rmspe variance) 
		avgwts(pop10)
		emin(-17) emax(33) 
		filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California_subsample") 
		filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY_subsample")
		savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att_subsample");
	#delimit cr
		
* Bias-corrected estimates
#delimit ;
	stackscpvals, 
		gap(gap_bc) 
		time(_time) 
		unit(cty_fips) 
		pvalues(rmspe variance) 
		avgwts(pop10)
		emin(-17) emax(33) 
		filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California_subsample") 
		filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY_subsample")
		savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att_subsample/bias_corrected")
		savename("bias_corr_att");
	#delimit cr
		
* Covid-corrected estimates
#delimit ;
	stackscpvals, 
		gap(p_gap) 
		time(_time) 
		unit(cty_fips) 
		pvalues(rmspe variance) 
		avgwts(pop10)
		emin(-17) emax(33) 
		filepath("$projdir/dta/analysis/qcew/`y'/naics`n'/California_subsample/corrected") 
		filepath_2("$projdir/dta/analysis/qcew/`y'/naics`n'/NY_subsample/corrected")
		savepath("$projdir/dta/analysis/qcew/`y'/naics`n'/nolocal_att_subsample/covid_corrected")
		savename("covid_corr_att");
	#delimit cr

	
* Close the log
log close sublog
